<?php
namespace Home\Controller;
use Think\Controller;
class ManageController extends Controller{
	public function _initialize(){
		if($_COOKIE["manager_id"] == null){
			$index = new IndexController();
			$index->login();exit;
		}
	}
	
	public function index(){
		
	}
	
	public function showManage(){
		$managenum = M("Manage")->count();
		$this->assign("managenum",$managenum);
		$this->display();
	}
	
	public function getManage(){
		$manage = M("Manage")->select();
		for($i=0;$i<count($manage);$i++){
			$id = $manage[$i]["id"];
			$manage[$i]["caozuo"]="<a href='/Home/Manage/deleteManage?id = $id'>删除</a>";
		}
		echo json_encode($manage);
	}
	
	public function addManage(){
		$this->display();
	}
	
	public function addOK(){
		$manage = M("Manage");
		
		if (strlen($_POST["password"]) < 6){
			$this->error("密码长度不足6位！",addManage,3);
		}
		
		$manage->name=$_POST["name"];
		$manage->managename = $_POST["managename"];
		$p=$_POST["password"];
		$manage->password = md5("$p");
		if($manage->add()){
			$this->success("新增成功",showManage,2);
		}else {
			$this->error("新增失败",addManage,3);
		}
	}
	
	public function deleteManage(){
		dump($_REQUEST);
	}
	
	public function changePassword(){
		 $this->display("changePw");
	}
	
	
	public function doChangePW(){
// 		dump($_POST);
// 		dump($_COOKIE);
// 		dump($_SESSION);
// 		exit;
		
		$id = $_SESSION['manager']['id'];
		
		$data = $_POST;
		$pw = M("Manage")->where("id = $id")->find()["password"];
		
		if($pw != md5($data["oldpassword"])){
			$this->error("原始密码不正确！",changePassword,3);
		}
		
		if ($data["password"] != $data["confirmpassword"]){
			$this->error("两次密码不一致！",changePassword,3);
		}
		
		if (strlen($data["password"]) < 6){
			$this->error("密码长度不足6位！",changePassword,3);
		}
		
		$manage = M("Manage");
		$manage->password = md5($data["password"]);
		if ($manage->where("id = $id")->save()){
			$this->success("修改成功","/Home/Index/index",2);
		}else{
			$this->error("修改失败",changePassword,3);
		}
		
	}
}